/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "VCBuffFIFO.h"

VCBuffFIFO::VCBuffFIFO(size_t start_size)
: m_in_offset(0)
, m_out_offset(0)
{
	size_t need_inc = (start_size > 0? start_size - 1 : 0) / DEF_VCBUFFFIFO_BLOCKSIZE + 1;
	m_buff_size_alloc = need_inc * DEF_VCBUFFFIFO_BLOCKSIZE;
	m_buff = (char *)malloc(m_buff_size_alloc);
}

VCBuffFIFO::~VCBuffFIFO()
{
	if (m_buff)
	{
		free(m_buff);
	}
}

bool VCBuffFIFO::Push(void * data, size_t data_size)
{
	bool result = false;
	if (data && data_size)
	{
		size_t need_size = m_in_offset + data_size;
		if (need_size > m_buff_size_alloc)
		{
			// need increase buff
			size_t need_inc = (need_size > 0? need_size - 1 : 0) / DEF_VCBUFFFIFO_BLOCKSIZE + 1;
			m_buff_size_alloc = need_inc * DEF_VCBUFFFIFO_BLOCKSIZE;
			m_buff = (char *)realloc(m_buff, m_buff_size_alloc);
		}
		memcpy(m_buff + m_in_offset, data, data_size);
		m_in_offset += data_size;
		result = true;
	}
	return result;
}

bool VCBuffFIFO::PushZero(size_t zero_size)
{
	bool result = false;
	if (zero_size)
	{
		size_t need_size = m_in_offset + zero_size;
		if (need_size > m_buff_size_alloc)
		{
			// need increase buff
			size_t need_inc = (need_size > 0? need_size - 1 : 0) / DEF_VCBUFFFIFO_BLOCKSIZE + 1;
			m_buff_size_alloc = need_inc * DEF_VCBUFFFIFO_BLOCKSIZE;
			m_buff = (char *)realloc(m_buff, m_buff_size_alloc);
		}
		memset(m_buff + m_in_offset, 0, zero_size);
		m_in_offset += zero_size;
		result = true;
	}
	return result;
}

bool VCBuffFIFO::Pop(void * data, size_t data_size)
{
	bool result = false;
	if (data_size && (data_size <= Size()))
	{
		if (data)
		{
			memcpy(data, m_buff + m_out_offset, data_size);
		}
		m_out_offset += data_size;
		if (m_out_offset)
		{
			size_t curr_size = Size();
			if (curr_size)
			{
				// need move data to start buff
				for (size_t i = 0; i < curr_size; i++)
				{
					m_buff[i] = m_buff[i + m_out_offset];
				}
			}
			m_out_offset = 0;
			m_in_offset = m_out_offset + curr_size;
		}
	}
	return result;
}

size_t VCBuffFIFO::Size()
{
	return (m_in_offset - m_out_offset);
}

bool VCBuffFIFO::Clear()
{
	return PopSize(Size());
}

void * VCBuffFIFO::GetDataPtr()
{
	void * result = NULL;
	if (Size())
	{
		result = m_buff + m_out_offset;
	}
	return result;
}

bool VCBuffFIFO::PopSize(size_t data_size)
{
	return Pop(NULL, data_size);
}

